package com.app.controller;

import com.app.model.AdminUserModel;
import com.app.model.ConfigModel;
import com.app.service.AdminUserService;
import com.app.service.ConfigService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 问卷调查选项配置
 * @Author: N
 * @Date: 2022/10/29 15:26
 */
@RestController
@RequestMapping("/")
public class ConfigController {
    @Autowired
    ConfigService configService;

    @Autowired
    private AdminUserService adminUserService;
    @RequestMapping(value = "/getAdminQuestionnaireConfig", method = RequestMethod.GET)
    public Result<Object> getAdminQuestionnaireConfig(HttpServletRequest request, Integer type){
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        ConfigModel model = new ConfigModel();
        model.setConType(type);
        List<ConfigModel> configModels = configService.getListByModel(model);
        List<ConfigModel> returnList = new ArrayList<>();
        if (type == 2 || type == 4) {
            for (ConfigModel configModel : configModels) {
                if(configModel.getConType().equals(2)||configModel.getConType().equals(4))
                {
                    if (configModel.getId().equals(user.getGroupId()) || configModel.getFatherId().equals(user.getGroupId())) {
                        returnList.add(configModel);
                    }
                }

            }
            if(user.getUserType().equals(4))
            {
                ConfigModel configModel = configService.getModelById(user.getGroupId());
                ConfigModel configModel1 = configService.getModelById(configModel.getFatherId());
                returnList.add(configModel1);
            }
            for (int i = 0; i < returnList.size(); i++) {
                if(returnList.get(i).getConType().equals(2)||returnList.get(i).getConType().equals(4))
                {
                    if (returnList.get(i).getConLevel().equals(2)) {
                        for (ConfigModel conModel : configModels) {
                            if(conModel.getFatherId().equals(returnList.get(i).getId()))
                            {
                                returnList.add(conModel);
                            }
                        }
                    }
                }
            }
        }
        if (type == 2 || type == 4) {
            return Result.success(findChildren(returnList, 0));
        }
        return Result.success(configModels);
    }

    @RequestMapping(value = "/getQuestionnaireConfig", method = RequestMethod.GET)
    public Result<Object> getAttentionList(Integer type){

        ConfigModel model = new ConfigModel();
        model.setConType(type);
        List<ConfigModel> configModels = configService.getListByModel(model);
        if(configModels!=null)
        {
            if(type==2||type==4)
            {
                return Result.success(findChildren(configModels,0));
            }
        }
        return Result.success(configModels);
    }

    @RequestMapping(value = "/getQuestionnaireConfigCus", method = RequestMethod.GET)
    public Result<Object> getQuestionnaireConfigCus(Integer type){
        ConfigModel model = new ConfigModel();
        model.setConType(type);
        List<ConfigModel> configModels = configService.getListByModel(model);
        ConfigModel model2 = new ConfigModel();
        model2.setConType(2);
        List<ConfigModel> configModels2 = configService.getListByModel(model2);
        configModels.addAll(configModels2);
        if(configModels!=null)
        {
            if(type==2||type==4)
            {
                return Result.success(findChildren2(configModels,0));
            }
        }
        return Result.success(configModels);
    }

    private List<ConfigModel> findChildren(List<ConfigModel> modelList, Integer pid){//递归方法
        List<ConfigModel> personModelList= new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if(Objects.equals(modelList.get(i).getFatherId(), pid))
            {
                ConfigModel model = modelList.get(i);
                List<ConfigModel> modelList1 = findChildren(modelList,model.getId());
                for (int j = 0; j < modelList1.size(); j++) {
                    if(modelList1!=null&&modelList1.size()>0)
                    {
                        model.setChild(modelList1);
                    }
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }
    private List<ConfigModel> findChildren2(List<ConfigModel> modelList, Integer pid){//递归方法
        List<ConfigModel> personModelList= new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if(Objects.equals(modelList.get(i).getFatherId(), pid))
            {
                ConfigModel model = modelList.get(i);
                List<ConfigModel> modelList1 = findChildren(modelList,model.getId());
                for (int j = 0; j < modelList1.size(); j++) {
                    if(modelList1!=null&&modelList1.size()>0)
                    {
                        model.setChildren(modelList1);
                    }
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }

}
